package handler

import (
	"context"
	"goods_server/basic/global"
	"goods_server/internal/model"
	__ "goods_server/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedGoodsServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) GoodsAdd(_ context.Context, in *__.GoodsAddRequest) (*__.GoodsAddResponse, error) {
	goods := model.Goods{
		GoodsNo:     in.GoodsNo,
		GoodsName:   in.GoodsName,
		GoodsText:   in.GoodsText,
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsNum:    uint(in.GoodsNum),
		GoodsImg:    in.GoodsImg,
		GoodsType:   in.GoodsType,
		GoodsStatus: in.GoodsStatus,
	}
	global.DB.Debug().Create(&goods)
	return &__.GoodsAddResponse{}, nil
}

// 发布选择规格
func (s *Server) GoodsItemAdd(_ context.Context, in *__.GoodsItemAddRequest) (*__.GoodsItemAddResponse, error) {
	goods := model.GoodsItems{
		GoodsId:    uint(in.GoodsId),
		LunchType:  in.LunchType,
		ItemNo:     in.ItemNo,
		Colour:     in.Colour,
		Place:      in.Place,
		UserPerson: in.UserPerson,
	}
	global.DB.Debug().Create(&goods)
	return &__.GoodsItemAddResponse{}, nil
}

// 发布其他设置
func (s *Server) GoodsOther(_ context.Context, in *__.GoodsOtherAddRequest) (*__.GoodsOtherAddResponse, error) {
	goods := model.Others{
		GoodsId:     uint(in.GoodsId),
		GoodsItemId: uint(in.GoodsItemId),
		Server:      in.Server,
		Status:      in.Status,
	}
	global.DB.Debug().Create(&goods)
	return &__.GoodsOtherAddResponse{}, nil
}

// 商品列表
func (s *Server) GoodsList(_ context.Context, in *__.GoodsListRequest) (*__.GoodsListResponse, error) {
	var GoodsList []model.Goods
	//page := in.Page
	//if page <= 0 {
	//	page = 1
	//}
	//
	//pageSize := in.Size
	//switch {
	//case pageSize > 100:
	//	pageSize = 100
	//case pageSize <= 0:
	//	pageSize = 10
	//}
	//
	//offset := (page - 1) * pageSize

	global.DB.Debug().Find(&GoodsList)
	var list []*__.GoodsList
	for _, v := range GoodsList {
		list = append(list, &__.GoodsList{
			Id:          int64(v.Id),
			GoodsNo:     v.GoodsNo,
			GoodsName:   v.GoodsName,
			GoodsText:   v.GoodsText,
			GoodsPrice:  float32(v.GoodsPrice),
			GoodsNum:    int64(v.GoodsNum),
			GoodsImg:    v.GoodsImg,
			GoodsType:   v.GoodsType,
			GoodsStatus: v.GoodsStatus,
		})
	}
	return &__.GoodsListResponse{List: list}, nil
}
